package Broker.yahooJapanAuction;

import java.io.*;
import java.util.ArrayList;
import model.Data;

/**
 *
 *
 * reference @author   jin
 *              Date: 2/3/09
 *              Connect to eBay website and read source
 *              HtmlSourceReaderImp
 * modified and renamed by: Raoux Etienne
 * Student login: jc218934
 * Student number: 12448190
 * email: etienne.raoux@my.jcu.edu.au
 * alt email: jc218934@gmail.com
 */
public class HtmlSourceReaderToList
{
    boolean ignore = true;
    boolean ignoreUrl = true;
    boolean done = false;
    String itemStart= "<!-- SET01 -->  <tr";
    String itemEnd = "</tr><!-- /SET01 -->";
    String historyStart = "<tr><td class=\"decBg01\">";
    String historyEnd = "</td></tr>";//"</span>";
    private ArrayList<String> itemList;
    private ArrayList<String> itemHistoryList;
    int itemIndex = -1;
    int historyIndex = -1;

    //Constructor
    public HtmlSourceReaderToList()
    {
        itemList = new ArrayList<String>();
        itemHistoryList = new ArrayList<String>();
    }

    //Task: Read given URL extract HTMl source code
    //Date: 2/3/09
    public ArrayList<String> readProducts(String url)
    {
        try
        {
            BufferedReader reader = getReader(url);
            
            String line = null;
            while( ( line = reader.readLine() ) != null )
            {
                if (line.contains(itemStart))
                {
                    ignore =false;
                    line = line.substring(line.indexOf(itemStart));
                    itemIndex++;
                }
                if (!ignore)
                {
                    String text = line;
                    if (line.contains(itemEnd))
                    {
                        line = line.substring(0, line.indexOf(itemEnd));
                        text += "\n";
                        ignore = true;
                    }
                    if (itemList.size() == itemIndex)
                        itemList.add(text);
                    else
                    {
                        itemList.set(itemIndex, itemList.get(itemIndex)+text);
                    }
                }

            }
            reader.close();
        }
        catch (Exception ex)
        {
        }
        return itemList;
    }

    public ArrayList<String> readHistory(String url)
    {
        String start = "|<a href=\"?aID=";
        String start2 = "|<a href=\"";
        String end = "\"";
        url+="&typ=log";
        try
        {
            BufferedReader reader = getReader(url);
            String nextUrl = "";

            String line = null;
            while( ( line = reader.readLine() ) != null )
            {
                if (line.contains(historyStart))
                {
                    ignore =false;
                    line = line.substring(line.indexOf(historyStart));
                    historyIndex++;
                }
                else if(line.contains(start))
                {
                    ignoreUrl =false;
                    line = line.substring(line.indexOf(start));
                }
                if (!ignore)
                {
                    String text = line;
                    if (line.contains(historyEnd))
                    {
                        line = line.substring(0, line.indexOf(historyEnd));
                        text += "\n";
                        ignore = true;
                    }
                    if (itemHistoryList.size() == historyIndex)
                        itemHistoryList.add(text);
                    else
                    {
                        itemHistoryList.set(historyIndex, itemHistoryList.get(historyIndex)+text);
                    }
                }
                else if (!ignoreUrl)
                {
                    if (!done)
                    {
                        String text = line;
                        if (line.contains(end))
                        {
                            line = line.substring(0, line.indexOf(end));
                            ignoreUrl = true;
                            done = true;
                        }
                        nextUrl += line;
                    }
                }

            }
            reader.close();
            if (done)
            {
                nextUrl = nextUrl.substring(nextUrl.indexOf(start2));
                itemHistoryList.addAll(readHistory(nextUrl));
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        return itemHistoryList;
    }

    private String nextBiddingHistoryUrlOfUrl(String url)
    {
        if (!url.contains("&typ=log#listtop"))
        {
            url = url.replaceAll("&typ=log", "&apg=2&typ=log#listtop");
        }
        else
        {
            String start = "&apg=";
            String end = "&typ=log#listtop";
            int pageNb = Integer.parseInt(url.substring(url.indexOf(start)+start.length(), url.indexOf(end)));
            url = url.replaceAll("&apg="+pageNb+"&typ=log#listtop"
                    , "&apg="+(pageNb+1)+"&typ=log#listtop");
        }
        System.out.println(url);
        return url;
    }

    //Connect to the URL server, open connection and get Reader (to Reader the source code)
    private static BufferedReader getReader(String url)
    {
        //Input String is recognized as URL
        if (url.startsWith("http://"))
        {
            try
            {
                //return new BufferedReader(new InputStreamReader(new URL(url).openConnection().getInputStream()));

                //Next line added for the presentation
                return new BufferedReader(new InputStreamReader(new File(new Data().getWebPage(url)).toURI().toURL().openStream()));
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        else //Input String is not recognized as URL
        {
            return null;
        }
    }
}
